<template>
	<view class="container">
		<uni-segmented-control :current="navIndex" :values="navList" style-type="text" @clickItem="onClickItem" />
		<view class="navBox navBox0" v-if="navIndex == 0">
			<view class="header">
				<dropDownMenu class="downMenu" :titleArr="title0" :dropArr="drop0" @finishDropClick="finishClick0"></dropDownMenu>
				<view class="total">数量：{{ total0 }}</view>
			</view>
			<view class="listTop">
				<picker mode="date" fields="month" :value="date" start="2021-01-01" @change="bindDateChange" @cancel="closeDate">
					<view class="date">{{ date }}</view>
				</picker>
				<view class="money">
					<view class="income">待收：{{ income }}</view>
				</view>
			</view>
			<view class="list list0" v-if="list0.length > 0">
				<view class="box" hover-class="hoverClass" v-for="item in list0" :key="item.id" @click.stop="clickBill(item)">
					<view class="top">
						<view class="left" @click.stop="callPhone(item)">
							<view class="name">
								{{ item.customer_name }}
							</view>
							<uni-icons type="phone-filled" size="18" color="#2979ff"></uni-icons>
						</view>
						<view class="right">{{ item.bill_title }}:{{ item.bill_money }}元</view>
					</view>
					<view class="bot">
						<view class="info">
							<view class="time">待收日期：{{ item.bill_date }}</view>
							<view class="address">
								{{ item.room_address }}
							</view>
						</view>
					</view>
				</view>
				<uni-pagination v-if="total0 > FormData0.perpage" :total="total0" :pageSize="FormData0.perpage" :value="FormData0.page" @change="changePage0" />
			</view>
			<view class="empty" v-else>
				<image class="image" src="@/static/img/empty.png" mode="aspectFit"></image>
				<view class="emptyWord">暂无数据</view>
			</view>
			<keyWord :words="FormData0.keyword" @onChange="setKeyWord0" />
		</view>
		<view class="navBox navBox1" v-else-if="navIndex == 1">
			<view class="header">
				<dropDownMenu class="downMenu" :titleArr="title1" :dropArr="drop1" @finishDropClick="finishClick1"></dropDownMenu>
				<view class="total">数量：{{ total1 }}</view>
			</view>
			<view class="list list1" v-if="list1.length > 0">
				<view class="box" hover-class="hoverClass" v-for="item in list1" :key="item.room_id" @click="goRentDetail(item)">
					<view class="top">
						{{ item.room_address }}
					</view>
					<view class="bot">
						<view class="text">
							<view>已收：{{ item.total_bills_payed }}笔</view>
							<view>已收金额：{{ item.total_money_payed }}元</view>
						</view>
						<view class="text">
							<view>待收：{{ item.total_bills_nopayed }}笔</view>
							<view>待收金额：{{ item.total_money_nopayed }}元</view>
						</view>
						<view class="text">租期：{{ item.start_date }} 至 {{ item.end_date }}</view>
					</view>
				</view>
				<uni-pagination v-if="total1 > FormData1.perpage" :total="total1" :pageSize="FormData1.perpage" :value="FormData1.page" @change="changePage1" />
			</view>
			<view class="empty" v-else>
				<image class="image" src="@/static/img/empty.png" mode="aspectFit"></image>
				<view class="emptyWord">暂无数据</view>
			</view>
			<keyWord :words="FormData1.keyword" @onChange="setKeyWord1" />
		</view>
	</view>
</template>

<script>
import api from '@/api/api';
export default {
	data() {
		return {
			date: '时间筛选',
			navList: ['待收账单', '待收账单汇总'],
			navIndex: 0,
			income: 0,
			FormData0: {
				keyword: '',
				group_id: '',
				start_time: '',
				end_time: '',
				bill_type: 'income',
				is_pay: 0,
				page: 1,
				perpage: 10
			},
			title0: ['分组', ''],
			drop0: [[]],
			total0: 0,
			list0: [],
			FormData1: {
				keyword: '',
				group_id: '',
				start_time: '',
				end_time: '',
				page: 1,
				perpage: 10
			},
			title1: ['分组', '状态', ''],
			drop1: [
				[],
				[
					{
						text: '全部',
						value: ''
					},
					{
						text: '今日录入',
						value: '1'
					},
					{
						text: '7日内录入',
						value: '7'
					},
					{
						text: '30日内录入',
						value: '30'
					}
				]
			],
			total1: 0,
			list1: []
		};
	},
	onLoad() {
		this.getGroup();
	},
	onShow() {
		this.getList();
	},
	onPullDownRefresh() {
		if (this.navIndex == 0) {
			this.FormData0.page = 1;
		} else if (this.navIndex == 1) {
			this.FormData0.page = 1;
		}
		this.getList();
	},
	methods: {
		clickBill(item) {
			console.log(item);
			const _this = this;
			uni.showActionSheet({
				itemList: ['完成收款', '催缴', '删除账单'],
				success: function (res) {
					if (res.tapIndex == 0) {
						uni.navigateTo({
							url: '/pages/add/setPayed?type=rent&id=' + item.id
						});
					} else if (res.tapIndex == 1) {
						console.log('催缴');
					} else if (res.tapIndex == 2) {
						uni.showModal({
							title: '提示',
							content: '确认删除该账单吗？',
							success: function (res) {
								if (res.confirm) {
									api.deleteBill({ bill_ids: item.id }).then((res) => {
										if (res.data.is_success) {
											uni.showToast({
												title: '删除成功！',
												icon: 'success'
											});
											_this.getList();
										}
									});
								}
							}
						});
					}
				}
			});
		},
		onClickItem(e) {
			this.navIndex = e.currentIndex;
			this.getList();
		},
		finishClick0(resultData) {
			this.FormData0.group_id = resultData[0];
			this.getList();
		},
		finishClick1(resultData) {
			this.FormData1.group_id = resultData[0];
			if (resultData[1] == '') {
				this.FormData1.end_time = '';
				this.FormData1.start_time = '';
			} else {
				this.FormData1.end_time = this.dayjs().format('YYYY-MM-DD');
				this.FormData1.start_time = this.dayjs(this.FormData1.end_time).subtract(resultData[1], 'd').format('YYYY-MM-DD');
			}
			this.getList();
		},
		setKeyWord0(e) {
			this.FormData0.keyword = e;
			this.getList();
		},
		setKeyWord1(e) {
			this.FormData1.keyword = e;
			this.getList();
		},
		bindDateChange(e) {
			this.date = e.detail.value;
			this.FormData0.start_time = this.date + '-01';
			this.FormData0.end_time = this.date + '-' + this.dayjs(this.FormData0.start_time).daysInMonth();
			this.getList();
		},
		closeDate(e) {
			this.date = '时间筛选';
			this.FormData0.start_time = '';
			this.FormData0.end_time = '';
			this.getList();
		},
		changePage0(e) {
			this.FormData0.page = e.current;
			this.getList();
		},
		changePage1(e) {
			this.FormData1.page = e.current;
			this.getList();
		},
		callPhone(item) {
			uni.makePhoneCall({
				phoneNumber: item.customer_mobile
			});
		},
		goRentDetail(item) {
			uni.navigateTo({
				url: '/pages/details/rent?sn=' + item.rent_sn
			});
		},
		getList() {
			if (this.navIndex == 0) {
				api.billList(this.FormData0).then((res) => {
					this.list0 = res.data.bill_data.items;
					this.total0 = res.data.bill_data.total;
					this.income = res.data.total_money_income;
					uni.stopPullDownRefresh();
				});
			} else if (this.navIndex == 1) {
				api.billListRoom(this.FormData1).then((res) => {
					this.list1 = res.data.items;
					this.total1 = res.data.total;
					uni.stopPullDownRefresh();
				});
			}
		},
		getGroup() {
			api.groupList({}).then((data) => {
				let list = data.data;
				let arr = [];
				for (let i = 0; i < list.length; i++) {
					let base = {
						text: list[i].group_name,
						value: list[i].id
					};
					arr.push(base);
				}
				let all = {
					text: '全部',
					value: ''
				};
				arr.unshift(all);
				this.drop0[0] = arr;
				this.drop1[0] = arr;
			});
		}
	}
};
</script>

<style lang="scss">
.segmented-control {
	background: #fff;
}
.navBox {
	.listTop {
		padding: 20rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		.date {
			display: flex;
			justify-content: center;
			align-items: center;
			font-size: 26rpx;
			padding: 6rpx 10rpx;
			background: #9a9a9a;
			border-radius: 20rpx;
			color: #fff;
			&::after {
				content: '';
				width: 0;
				height: 0;
				border: 10rpx solid transparent;
				border-top-color: #fff;
				margin-top: 15rpx;
				margin-left: 6rpx;
			}
		}
		.money {
			font-size: 24rpx;
			line-height: 1.4;
			.income {
				color: #1ecb6b;
			}
			.payed {
				color: #ee5959;
			}
		}
	}
	.list {
		padding: 0 20rpx;
		.box {
			background: #fff;
			margin-bottom: 20rpx;
			border-radius: 20rpx;
		}
	}
	.list0 {
		.box {
			.top {
				display: flex;
				justify-content: space-between;
				align-items: center;
				border-bottom: 1rpx solid $uni-border-2;
				padding: 20rpx;
				font-weight: bold;
				.left {
					display: flex;
					align-items: center;
					.name {
						margin-right: 10rpx;
						font-size: 28rpx;
					}
				}
				.right {
					font-size: 28rpx;
				}
			}
			.bot {
				display: flex;
				justify-content: space-between;
				padding: 20rpx;
				.info {
					font-size: 26rpx;
					line-height: 1.6;
				}
			}
		}
	}
	.list1 {
		padding: 20rpx;
		.box {
			.top {
				border-bottom: 1rpx solid $uni-border-2;
				padding: 20rpx;
				font-weight: bold;
				font-size: 30rpx;
			}
			.bot {
				padding: 20rpx;
				font-size: 26rpx;
				line-height: 1.8;
				.text {
					display: flex;
					view {
						min-width: 200rpx;
					}
				}
			}
		}
	}
}
.navBox0 {
	.header .total {
		width: 50%;
	}
}
</style>
